//
//  MainMenuView.m
//  LabanBrowser
//
//  Created by Xh2 on 3/26/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "MainMenuView.h"
#import "MenuBoard.h"
#import "MenuDataProvider.h"
#import "MenuDataModel.h"
#import "StaticVariables.h"
#import "MyUtilities.h"
@implementation MainMenuView{
    NSIndexPath* displayModeIndexPath;
}
@synthesize menuViewArray;
@synthesize menuDataProvider;
@synthesize pageControl;
@synthesize scrollView;
@synthesize mainDelegate;
@synthesize backgroundImageView;

int kNumberOfMenuBoard=1;
double kMainMenuFadeDuration=0.2f;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        double dOffset=0;
        backgroundImageView=[[UIImageView alloc]initWithFrame:CGRectMake(-dOffset, -dOffset, frame.size.width+dOffset*2, frame.size.height+dOffset*2) ];
        UIImage* backgroundImage = [[UIImage imageNamed:@"bg_browser_popup_dialog.png"]stretchableImageWithLeftCapWidth:230 topCapHeight:50];
        //[backgroundImage ;
        [backgroundImageView setImage:backgroundImage];
        [self addSubview:backgroundImageView];

        
        
        // Initialization code
        menuDataProvider=[[MenuDataProvider alloc]init];
        menuViewArray=[[NSMutableArray alloc]init];
        CGRect scrollRect=CGRectMake(0, 0, frame.size.width, frame.size.height);
        scrollView=[[UIScrollView alloc]initWithFrame:scrollRect];
        [scrollView setPagingEnabled:YES];
        [scrollView setShowsHorizontalScrollIndicator:NO];
        scrollView.clipsToBounds=YES;
        //scrollView.bounces=NO;
        scrollView.scrollsToTop = NO;
        scrollView.bounces=NO;
        scrollView.delaysContentTouches=NO;
        scrollView.canCancelContentTouches=YES;
        scrollView.scrollEnabled=YES;
        [self addSubview: scrollView];
        scrollView.delegate=self;
        
        
        //self.scrollView.layer.borderWidth=1;
        //self.scrollView.layer.borderColor=[[UIColor blackColor]CGColor];
        //self.backgroundColor=[UIColor blackColor];
        
        
        for(int i=0;i<menuDataProvider.menuData.count;i++){
            CGRect iRect=CGRectMake(frame.size.width*i, 0, frame.size.width, frame.size.height);
            MenuBoard* iBoard=[[MenuBoard alloc]initWithFrame:iRect];
            iBoard.tag=i;
            iBoard.dataSource=self;
            iBoard.delegate=self;
            [scrollView addSubview:iBoard];
            [menuViewArray addObject:iBoard];
        }
        
        [scrollView setContentSize:CGSizeMake(frame.size.width*menuDataProvider.menuData.count, frame.size.height)];
        double pageControlHeight=24;
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height-pageControlHeight-2, frame.size.width, pageControlHeight)];
        [self addSubview:pageControl];
        pageControl.numberOfPages=menuDataProvider.menuData.count;
        
        self.alpha=0;
        
        self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        //[scrollView scrollRectToVisible:dialer.frame animated:NO];
               
    }
    return self;
}

#pragma mark Private Method
-(void)show{

        [UIView animateWithDuration:kMainMenuFadeDuration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.alpha=1;
                         } completion:^(BOOL finished) {
                             
                         }];

    
}
-(void)hide{
    
        [UIView animateWithDuration:kMainMenuFadeDuration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.alpha=0;
                         } completion:^(BOOL finished) {
                             
                         }];
    
    
}

-(void)autoShowOrHide{
    if(self.alpha==0){
        [self show];
    }else{
        [self hide];
    }
}
-(void)reloadDisplayMode{
    MenuBoard* firstBoard=[menuViewArray objectAtIndex:0];
    //[secondBoard performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    
    [firstBoard performBatchUpdates:^{
        [firstBoard reloadItemsAtIndexPaths:@[displayModeIndexPath]];
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)refresh{
    [menuDataProvider reload];
    for(int i=0;i<menuViewArray.count;i++){
        MenuBoard* iBoard=[menuViewArray objectAtIndex:i];
        //[iBoard performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        [iBoard reloadData];
    }
}

#pragma mark scrollView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat pageWidth = self.scrollView.frame.size.width; // you need to have a **iVar** with getter for scrollView
    float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pageControl.currentPage = page;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

#pragma collectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [[menuDataProvider.menuData objectAtIndex:collectionView.tag] count];

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuBoard* menuBoard=(MenuBoard*)collectionView;
    TextButton* currentButton=[menuBoard dequeueReusableCellWithReuseIdentifier:@"menuBoard" forIndexPath:indexPath];

    MenuDataModel* currentMenuModel=[[menuDataProvider.menuData objectAtIndex:menuBoard.tag] objectForKey:[NSNumber numberWithInt:indexPath.row]];
        
    if(currentMenuModel.menuType==MenuNightMode){
        displayModeIndexPath=indexPath;
        NSNumber* currentMode=[[[StaticVariables shareVariables]userDefaults]webDisplayMode];
        
        //currentButton.backgroundBT.enabled=NO;
        if(currentMode.intValue == NightMode){
            [currentButton.iconImage setImage:[UIImage imageNamed:@"ic_browser_menu_daymode_nor.png"]];
            currentButton.titleLB.text=[MyUtilities getLocalizeStringFromTag:@"MenuDaymode"];


        }else{
            [currentButton.iconImage setImage:currentMenuModel.iconImage];
            currentButton.titleLB.text=currentMenuModel.title;

        }
    }else{
        currentButton.titleLB.text=currentMenuModel.title;

        [currentButton.iconImage setImage:currentMenuModel.iconImage];
    }
    
    currentButton.backgroundBT.highlighted=NO;
    currentButton.menuOption=currentMenuModel.menuType;
    currentButton.delegate=mainDelegate;
    
    return currentButton;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}



@end
